Week 11 — Graph Algorithms
1. Longest Path
For a weighted graph, the length of a path is the sum of the weights of its edges.
For a path
its total weight is
The longest path is a path with maximum total weight among the paths being considered.
2. Longest Path in a DAG
For a directed acyclic graph (DAG), longest paths can be found using a topological ordering.
For an edge
the longest-path value can be updated as
Processing vertices in topological order ensures that the required predecessor values have already been computed.
Transitive Closure
3. Reachability
A vertex is reachable from vertex if there exists a path from to .
4. Transitive Closure
The transitive closure of a directed graph represents reachability between every pair of vertices.
For vertices and ,
5. Transitive Closure Using Boolean Matrices
The adjacency matrix can be extended to represent paths of greater length.
Matrix operations can therefore be used to determine reachability.
Matrix Multiplication
6. Matrix Product
For matrices and , the product is defined by
7. Matrix Dimensions
If
and
then
8. Matrix Multiplication and Graphs
For an adjacency matrix , powers of the matrix can represent paths of different lengths.
The entry
can be used to determine the number of walks of length from vertex to vertex .
Single-Source Shortest Paths
9. Shortest Path
The shortest path between two vertices is a path with minimum total edge weight.
For a source vertex , the single-source shortest-path problem finds the shortest distance from to every reachable vertex.
10. Relaxation
For an edge
with weight , relaxation checks whether going through gives a shorter path to .
If so, update:
Dijkstra's Algorithm
11. Dijkstra's Algorithm
Dijkstra's algorithm solves the single-source shortest-path problem for graphs with non-negative edge weights.
12. Dijkstra Initialization
For source vertex :
For every other vertex :
13. Dijkstra's Algorithm
Repeatedly:
- Select the unprocessed vertex with the smallest tentative distance.
- Mark it as processed.
- Relax its outgoing edges.
- Continue until all required vertices are processed.
14. Dijkstra Complexity
Using an adjacency list and priority queue, a common implementation has complexity
Bellman-Ford Algorithm
15. Bellman-Ford Algorithm
Bellman-Ford solves the single-source shortest-path problem and can handle negative edge weights.
It can also detect reachable negative-weight cycles.
16. Bellman-Ford Relaxation
All edges are repeatedly relaxed.
For a graph with vertices, the edges are relaxed times.
17. Negative Cycle Detection
After relaxation passes, perform one additional pass.
If any distance can still be improved, a reachable negative-weight cycle exists.
18. Bellman-Ford Complexity
The time complexity is
All-Pairs Shortest Paths
19. All-Pairs Shortest Paths
The all-pairs shortest-path problem finds the shortest distance between every pair of vertices.
The result can be represented by a distance matrix :
Floyd-Warshall Algorithm
20. Floyd-Warshall Algorithm
Floyd-Warshall is a dynamic-programming algorithm for the all-pairs shortest-path problem.
21. Floyd-Warshall Recurrence
Let represent the shortest distance from to using only the first vertices as intermediate vertices.
Then:
22. Floyd-Warshall Complexity
The time complexity is
Minimum-Cost Spanning Trees
23. Spanning Tree
A spanning tree of a connected undirected graph:
- Contains every vertex.
- Is connected.
- Contains no cycles.
For a graph with vertices, every spanning tree contains
edges.
24. Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a spanning tree whose total edge weight is minimum.
Its total cost is
where is the selected spanning tree.
Prim's Algorithm
25. Prim's Algorithm
Prim's algorithm constructs an MST by repeatedly adding the minimum-weight edge that connects a vertex already in the tree to a vertex outside the tree.
26. Prim's Algorithm
Starting from any vertex:
- Add the starting vertex to the tree.
- Find the minimum-weight edge connecting the tree to an unvisited vertex.
- Add that edge and vertex.
- Repeat until all vertices are included.
27. Prim's Complexity
Using an adjacency list and priority queue, a common implementation has complexity
Kruskal's Algorithm
28. Kruskal's Algorithm
Kruskal's algorithm constructs an MST by considering edges in increasing order of weight.
29. Kruskal's Algorithm
- Sort all edges by increasing weight.
- Consider each edge in that order.
- Add the edge if it does not create a cycle.
- Continue until edges have been selected.
30. Cycle Detection
Kruskal's algorithm commonly uses a disjoint-set (Union-Find) data structure to determine whether adding an edge would create a cycle.
31. Kruskal's Complexity
Sorting the edges takes
Thus, the overall complexity is commonly written as
Algorithm Comparison
| Problem | Algorithm | Important Condition | Complexity | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Single-source shortest path | Dijkstra | Non-negative edge weights | $O(( | V | + | E | )\log | V | )$ |
| Single-source shortest path | Bellman-Ford | Allows negative edges | $O( | V | E | )$ | |||
| All-pairs shortest paths | Floyd-Warshall | — | $O( | V | ^3)$ | ||||
| Minimum spanning tree | Prim | Weighted undirected graph | $O(( | V | + | E | )\log | V | )$ |
| Minimum spanning tree | Kruskal | Weighted undirected graph | $O( | E | \log | E | )$ |
Formula Reference
| Topic | Key Result | ||||||
|---|---|---|---|---|---|---|---|
| Path weight | |||||||
| Longest-path relaxation | |||||||
| Transitive closure | if is reachable from | ||||||
| Matrix multiplication | |||||||
| Relaxation | |||||||
| Dijkstra initialization | |||||||
| Bellman-Ford passes | $ | V | -1$ | ||||
| Bellman-Ford complexity | $O( | V | E | )$ | |||
| Floyd-Warshall recurrence | |||||||
| Floyd-Warshall complexity | $O( | V | ^3)$ | ||||
| Edges in spanning tree | $ | V | -1$ | ||||
| Prim complexity | $O(( | V | + | E | )\log | V | )$ |
| Kruskal complexity | $O( | E | \log | E | )$ |